//
//  WJQUserViewController.m
//  TripTogether
//
//  Created by dllo on 16/1/4.
//  Copyright © 2016年 杨泓晔. All rights reserved.
//

#import "WJQUserViewController.h"
#import "MBTwitterScroll.h"
#import <BmobSDK/Bmob.h>
#import <SMS_SDK/SMSSDK.h>

#import "YHYUserAboutViewController.h"

#import "WJQUserLoginViewController.h"

@interface WJQUserViewController () <UITableViewDataSource, UITableViewDelegate, MBTwitterScrollDelegate>

@property (nonatomic, retain) MBTwitterScroll *userMB;
@property (nonatomic, retain) UIImageView *headImageV;
@property (nonatomic, strong) UIButton *headBtn;

@end

@implementation WJQUserViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.leftBarButtonItem = nil;
    // 状态栏
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    self.userMB = [[MBTwitterScroll alloc] initTableViewWithBackgound:[UIImage imageNamed:@"wode_morenbg@2x.jpg"]
                                                          avatarImage:[UIImage imageNamed:@"wode_morenbg@2x.jpg"]
                                                          titleString:@"我的"
                                                       subtitleString:@""
                                                          buttonTitle:nil];
    
    self.userMB.tableView.delegate = self;
    self.userMB.tableView.dataSource = self;
    self.userMB.delegate = self;
//    self.userMB.tableView.bounces = NO;
    self.userMB.tableView.tableFooterView = [UIView new];
        [self.userMB.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.userMB];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapAction)];
    [self.userMB.avatarImage addGestureRecognizer:tap];
    

    _headBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 100, 120, 80, 35)];
    [_headBtn setTitle:@"登陆" forState:UIControlStateNormal];
    [_headBtn setTitle:@"注销" forState:UIControlStateSelected];
    [_headBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    _headBtn.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Medium" size:12];
    _headBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _headBtn.layer.borderWidth = 1;
    _headBtn.layer.cornerRadius = 8;
    [_headBtn addTarget:self action:@selector(headBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.userMB.tableView addSubview:_headBtn];
    
    

    
    
    _headBtn.selected = NO;
    
    [self refurbish];
    
    // 接收刷新通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refurbish) name:@"refurbish" object:nil];
}
- (void)refurbish
{
    BmobUser *bUser = [BmobUser getCurrentUser];
    if (bUser) {
      
        [self.userMB.avatarImage sd_setImageWithURL:[NSURL URLWithString:[bUser objectForKey:@"imageurl"]] placeholderImage:PICHOLDEER];
        
        _headBtn.selected = YES;
        
        self.userMB.titleLabel.text = [bUser objectForKey:@"username"];
        self.userMB.headerLabel.text = [bUser objectForKey:@"username"];
    }else{
        
        self.userMB.avatarImage.image = [UIImage imageNamed:@"wode_morenbg@2x.jpg"];
        
        _headBtn.selected = NO;
        
        self.userMB.titleLabel.text = @"我的";
        self.userMB.headerLabel.text = @"我的";
    }
}
- (void)imageTapAction
{
    NSLog(@"eee");
}

-(void)headBtnAction
{
    if (_headBtn.selected) {
        
        
        [self alertControllerWithTitle:@"注销" message:@"确定要注销吗?" block:^{
            
            [BmobUser logout];
            [self refurbish];
        }];
        
        
        
    } else {
        WJQUserLoginViewController *loginVC = [[WJQUserLoginViewController alloc] init];
        
        [self presentViewController:loginVC animated:YES completion:^{
            
            
        }];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10.f;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (0 == section) {
        return 2;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    
    if (0 == indexPath.section) {
        
        if (0 == indexPath.row) {
            cell.textLabel.text = @"我的收藏";
        } else {
            cell.textLabel.text = @"清除缓存";
        }
    } else {
        cell.textLabel.text = @"关于我们";
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 选中时有置灰效果, 离开时消失选中效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    if (0 == indexPath.section) {
        
        if (0 == indexPath.row) {
           
            
        } else {
            // 清除缓存
            [self clearCaches];
        }
    } else {
        YHYUserAboutViewController *userVC = [YHYUserAboutViewController new];
        
        [self.navigationController pushViewController:userVC animated:YES];
    }
}
#pragma mark - 清除缓存
- (void)clearCaches
{
    // 获取SDWebImage缓存图片大小
    NSUInteger size = [[SDImageCache sharedImageCache] getSize];
    float cacheSize = size / 1024.0 / 1024.0;
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"清除缓存"message:[NSString stringWithFormat:@"缓存大小为: %0.2f MB", cacheSize] preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 清除缓存
        [[SDImageCache sharedImageCache] clearDisk];
        NSLog(@"缓存大小为:%0.2f MB", cacheSize);
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"缓存清除成功";
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertC addAction:action2];
    [alertC addAction:action1];
    
    [self presentViewController:alertC animated:YES completion:nil];
    
    
    
}




-(void)recievedMBTwitterScrollEvent
{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
